<?php

namespace app\command\command;

use app\api\service\UAService;
use app\api\service\BaiduService;
use app\command\model\IPModel;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;

class Visit extends Command {
    protected function configure() {
        $this->setName('Visit');
    }

    protected function execute(Input $input, Output $output) {
        $UAService    = new UAService();
        $baiduService = new BaiduService();
        $IPModel      = new IPModel();
        $visitList    = Db::table('tps_visited')->field('id,user_agent,operation,browser,ip')->where(['browser' => null])->select();
        foreach ($visitList AS $visit) {
            //维护访问记录
            $data = $UAService->getClientInfo($visit['user_agent']);
            Db::table('tps_visited')->where(['id' => $visit['id']])->update($data);
            //查询用户的IP信息
            $IPHis  = Db::table('tps_ip')->where(['ip' => $visit['ip']])->count();
            $IPList = [];
            if (empty($IPHis) && empty($IPList[$visit['ip']])) {
                $ipInfo = $baiduService->getAddressByIp($visit['ip']);
                if ($ipInfo['status'] == 0) {
                    $ipData                      = $ipInfo['content']['address_detail'];
                    $ipData['ip']                = $visit['ip'];
                    $ipData['address']           = $ipInfo['content']['address'];
                    $ipData['address_longitude'] = $ipInfo['content']['point']['x'];
                    $ipData['address_latitude']  = $ipInfo['content']['point']['y'];
                    $adddressData                = explode('|', $ipInfo['address']);
                    $ipData['operator']          = $adddressData[4];
                    $IPList[$visit['ip']]        = $IPModel->create($ipData);
                }
            }
        }
        //维护url
        $urlList = Db::table('tps_url')->field('id,url,domain')->where(['status' => 0])->select();
        foreach ($urlList AS $url) {
            if (in_array($url['domain'], ['houannj.com/', 'xbj.hs/'])) {
                $url['url'] = strtolower($url['url']);
                $urlInfo    = explode('//', $url['url']);
                $urlInfo    = explode('/', $urlInfo[1]);
                if (count($urlInfo) == 2) {
                    $urlData ['status'] = 1;
                    $urlInfo[1]         = explode('.', $urlInfo[1])[0];
                    $urlData['action']  = 'lists';
                    $urlData['data_id'] = '';
                    $urlData['module']  = $urlInfo[1];
                    if (in_array($urlInfo[1], ['news', 'scenery'])) {
                        $urlData['status'] = 1;
                    } else {
                        $urlData['status'] = 2;
                    }
                } elseif (count($urlInfo) == 3) {
                    $urlData ['status'] = 1;
                    $urlInfo[2]         = explode('.', $urlInfo[2])[0];
                    $urlData['action']  = 'details';
                    $urlData['data_id'] = intval($urlInfo[2]);
                    $urlData['module']  = $urlInfo[1];
                    if (in_array($urlInfo[1], ['news', 'scenery'])) {
                        $urlData['status'] = 1;
                    } else {
                        $urlData['status'] = 2;
                    }
                } else {
                    $urlData['status'] = 2;
                }
            } else {
                $urlData['status'] = 3;
            }
            Db::table('tps_url')->where(['id' => $url['id']])->update($urlData);
            $urlData = [];
        }
    }
}